﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.BaseDictionaryService;
using Model;
using BLL.PayRecordsTransaction;

namespace FamilyManager
{
    public partial class PayRecordInputForm : BaseForm
    {

        private PayRecordInputForm()
        {
            InitializeComponent();
        }

        private static PayRecordInputForm instance;

        public static PayRecordInputForm CreateInstance()
        {
            if (instance == null)
            {
                instance = new PayRecordInputForm();
            }
            return instance;
        }

        private void PayRecordInputForm_Load(object sender, EventArgs e)
        {
            BindcbPayCategory();
            BindcbPayNecessity();
            BindcbUsers();
            BindgvPayRecords();
        }

        private void BindgvPayRecords()
        {
            gvResults.AutoGenerateColumns = false;
            gvResults.Columns.Clear();

            QueryPayRecordsByTimeTransaction objQueryPayRecordsByTimeTransaction = new QueryPayRecordsByTimeTransaction(DateTime.Parse(dtpPayDate.Text), DateTime.Parse(dtpPayDate.Text));
            //objQueryPayRecordsByTimeTransaction.Execute();

            gvResults.DataSource = objQueryPayRecordsByTimeTransaction.FiltPayRecords;

            DataGridViewTextBoxColumn dcOID = new DataGridViewTextBoxColumn();
            dcOID.HeaderText = "记录序号";
            dcOID.DataPropertyName = "Db_OID";
            gvResults.Columns.Add(dcOID);
            dcOID.Visible = false;

            DataGridViewTextBoxColumn dcPayDate = new DataGridViewTextBoxColumn();
            dcPayDate.HeaderText = "消费日期";
            dcPayDate.DataPropertyName = "Db_PayDate";
            gvResults.Columns.Add(dcPayDate);

            DataGridViewTextBoxColumn dcPayItem = new DataGridViewTextBoxColumn();
            dcPayItem.HeaderText = "消费项目";
            dcPayItem.DataPropertyName = "Db_PayItem";
            gvResults.Columns.Add(dcPayItem);


            DataGridViewTextBoxColumn dcPayObject = new DataGridViewTextBoxColumn();
            dcPayObject.HeaderText = "消费对象";
            dcPayObject.DataPropertyName = "UserNickName";
            gvResults.Columns.Add(dcPayObject);


            DataGridViewTextBoxColumn dcPayCategoryOID = new DataGridViewTextBoxColumn();
            dcPayCategoryOID.HeaderText = "消费类别";
            dcPayCategoryOID.DataPropertyName = "PayCategory";
            gvResults.Columns.Add(dcPayCategoryOID);

            DataGridViewTextBoxColumn dcPaySum = new DataGridViewTextBoxColumn();
            dcPaySum.HeaderText = "消费金额";
            dcPaySum.DataPropertyName = "Db_PaySum";
            gvResults.Columns.Add(dcPaySum);

            DataGridViewTextBoxColumn dcPayNecessity = new DataGridViewTextBoxColumn();
            dcPayNecessity.HeaderText = "必要性";
            dcPayNecessity.DataPropertyName = "PayNecessity";
            gvResults.Columns.Add(dcPayNecessity);

            DataGridViewTextBoxColumn dcPayMemo = new DataGridViewTextBoxColumn();
            dcPayMemo.HeaderText = "消费备注";
            dcPayMemo.DataPropertyName = "Db_PayMemo";
            gvResults.Columns.Add(dcPayMemo);

            
        }

        private void BindcbUsers()
        {
            cbUsers.Items.Insert(0, "");
            QueryUsersTransaction command = new QueryUsersTransaction();
            command.Execute();
            cbUsers.DataSource = command.Users;
            cbUsers.DisplayMember = "Db_NickName";
            cbUsers.ValueMember = "Db_OID";

            
        }

        private void BindcbPayNecessity()
        {
            QueryPayNecessityTransaction command = new QueryPayNecessityTransaction();
            command.Execute();
            cbPayNecessity.DataSource = command.PayNecessitys;
            cbPayNecessity.DisplayMember = "Db_PayNecessity";
            cbPayNecessity.ValueMember = "Db_OID";
        }

        private void BindcbPayCategory()
        {
            QueryPayCategoryTransaction command = new QueryPayCategoryTransaction();
            command.Execute();
            this.cbPayCategory.DataSource = command.PayCategorys;
            this.cbPayCategory.ValueMember = "Db_OID";
            this.cbPayCategory.DisplayMember = "Db_CategoryName";
        }

        private bool CheckInfo()
        {
            if (dtpPayDate.Text == string.Empty)
            {
                MessageBox.Show("请输入消费日期！");
                dtpPayDate.Focus();
                return false;
            }
            if (txtPayItem.Text == string.Empty)
            {
                MessageBox.Show("请输入消费项目！");
                txtPayItem.Focus();
                return false;
            }
            if (cbPayNecessity.Text == string.Empty)
            {
                MessageBox.Show("请输入消费必要性！");
                cbPayNecessity.Focus();
                return false;
            }
            if (cbPayCategory.Text == string.Empty)
            {
                MessageBox.Show("请输入消费类别！");
                cbPayCategory.Focus();
                return false;
            }
            if (cbUsers.Text == string.Empty)
            {
                MessageBox.Show("请输入消费对象！");
                cbUsers.Focus();
                return false;
            }
            double tmp=0.0;
            if (txtPaySum.Text == string.Empty )
            {
                MessageBox.Show("请输入消费金额！");
                txtPaySum.Focus();
                return false;
            }
            if (!double.TryParse(txtPaySum.Text, out tmp))
            {
                MessageBox.Show("请输入正确的消费金额！");
                txtPaySum.Focus();
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckInfo())
            {
                T_PayRecordsObject objPayRecord = new T_PayRecordsObject();
                objPayRecord.Db_PayDate = DateTime.Parse(DateTime.Parse(dtpPayDate.Text).ToShortDateString());
                objPayRecord.Db_PayItem = txtPayItem.Text;
                objPayRecord.Db_PayNecessity = int.Parse(cbPayNecessity.SelectedValue.ToString());
                objPayRecord.Db_PayCategoryOID = int.Parse(cbPayCategory.SelectedValue.ToString());
                objPayRecord.Db_PayObject = int.Parse(cbUsers.SelectedValue.ToString());
                objPayRecord.Db_PaySum = decimal.Parse(txtPaySum.Text);
                objPayRecord.Db_PayMemo = txtPayMemo.Text;


                    AddPayRecordsTransaction objAddPayRecordsTransaction = new AddPayRecordsTransaction(objPayRecord);
                    objAddPayRecordsTransaction.Execute();


                BindgvPayRecords();

                txtPayItem.Text = string.Empty;
                txtPaySum.Text = string.Empty;
                txtPayMemo.Text = string.Empty;

            }
        }

        private void dtpPayDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode== Keys.Tab)
            {
                cbPayNecessity.Focus();
            }
        }

        private void cbPayNecessity_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                txtPayItem.Focus();
            }
        }

        private void txtPayItem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                cbPayCategory.Focus();
            }
        }

        private void cbPayCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                txtPaySum.Focus();
            }
        }

        private void txtPaySum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                cbUsers.Focus();
            }
        }

        private void cbUsers_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                txtPayMemo.Focus();
            }
        }

 

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count == 0) return;

            DataGridViewRow row = gvResults.SelectedRows[0];
            string s = row.Cells[0].Value.ToString();
            LoadSinglePayRecordsTransaction objLoadSinglePayRecordsTransaction = new LoadSinglePayRecordsTransaction(int.Parse(s));
            //objLoadSinglePayRecordsTransaction.Execute();
            T_PayRecordsObject ResultRecord = objLoadSinglePayRecordsTransaction.ResultRecord;
            DialogResult result = MessageBox.Show("确认删除当前记录?", "提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                DeletePayRecordsTransaction objDeletePayRecordsTransaction = new DeletePayRecordsTransaction(int.Parse(s));
                objDeletePayRecordsTransaction.Execute();
                if (objDeletePayRecordsTransaction.Result == true)
                {
                    BindgvPayRecords();
                    MessageBox.Show("删除成功!已删除信息：消费项目:" + ResultRecord.Db_PayItem + "   消费金额:" + ResultRecord.Db_PaySum + "   消费人:" + ResultRecord.UserNickName);
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
        }



        private void txtPayMemo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                btnSave.Focus();
            }
        }





        private void dtpPayDate_ValueChanged(object sender, EventArgs e)
        {
            BindgvPayRecords();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStatistic_Click(object sender, EventArgs e)
        {
            if (gvResults.Rows.Count == 0) return;

            IList<int> recordsOIDs = new List<int>();
            foreach (DataGridViewRow row in gvResults.Rows)
            {
                recordsOIDs.Add(int.Parse(row.Cells[0].Value.ToString()));
            }
            PayRecordStatisticsForm objPayRecordStatisticsForm = new PayRecordStatisticsForm(recordsOIDs);
            objPayRecordStatisticsForm.ShowDialog();
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gvResults.Rows.Count == 0) return;

            DataGridViewRow row = gvResults.SelectedRows[0];
            string s = row.Cells[0].Value.ToString();
            PayRecordEditForm objPayRecordEditForm = new PayRecordEditForm(int.Parse(s));
            if (objPayRecordEditForm.ShowDialog() == DialogResult.Yes)
            {
                BindgvPayRecords();
            }
        }
    }
}
